//
//  UICustomPickerView.m
//  nonghubao
//
//  Created by Fenrir on 13-6-23.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "UICustomPickerView.h"
#import "Config.h"

@implementation UICustomPickerView
@synthesize normalPickerView = _normalPickerView;
@synthesize toolBar = _toolBar;
@synthesize dataSource = _dataSource;
@synthesize delegate = _delegate;
@synthesize selectedRow = _selectedRow;
@synthesize selectedComponent = _selectedComponent;
@synthesize areaPickerView = _areaPickerView;
@synthesize hzAreaPickerStyle = _hzAreaPickerStyle;
@synthesize areaPickerOption = _areaPickerOption;
@synthesize location = _location;
@synthesize datePickerView = _datePickerView;
@synthesize currentDate = _currentDate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void)pickerShow:(UICustomPickerViewType)type
{
    _type = type;
    
    if (_type == UICustomPickerViewNormalType) {
        
        if (_normalPickerView == nil) {
            _normalPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,216, 320, 216)];
        }
        if (iOS7) {
            _normalPickerView.backgroundColor = [UIColor whiteColor];
            
        }
        _normalPickerView.showsSelectionIndicator = YES;
        _normalPickerView.delegate = self;
        _normalPickerView.dataSource = self;
        
        [self addSubview:_normalPickerView];
        
        [self addToolBar:_normalPickerView];
        
        [UIView animateWithDuration:0.5 animations:^{
            _normalPickerView.frame = CGRectMake(0, 44, 320, 216);
            _toolBar.frame = CGRectMake(0, _normalPickerView.frame.origin.y-44, 320, 44);
        }];
    }
    
    else if (_type == UICustomPickerViewAreaType) {
        
        if (_areaPickerView == nil) {
            
            _areaPickerView = [[HZAreaPickerView alloc] initWithStyle:_hzAreaPickerStyle delegate:self withOption:_areaPickerOption];
            
            _areaPickerView.frame = CGRectMake(0,216, 320, 216);
        }
        if (_location != nil) {
            [_areaPickerView selectPickerView:_location];
        }
        
        [self addSubview:_areaPickerView];
        
        [self addToolBar:_areaPickerView];
        
        [UIView animateWithDuration:0.5 animations:^{
            _areaPickerView.frame = CGRectMake(0, 44, 320, 216);
            _toolBar.frame = CGRectMake(0, _areaPickerView.frame.origin.y-44, 320, 44);
        }];
    }
    
    else if (_type == UICustomPickerViewDateType) {
        
        if (_datePickerView == nil) {
            _datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,216, 320, 216)];
        }
        
        if (iOS7) {
            _datePickerView.backgroundColor = [UIColor whiteColor];

        }
        // 设置时区
        [_datePickerView setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
        
        _datePickerView.locale = [NSLocale currentLocale];
        
        // 设置显示最大时间（此处为当前时间）
        [_datePickerView setMaximumDate:[NSDate date]];
        
        // 设置UIDatePicker的显示模式
        [_datePickerView setDatePickerMode:UIDatePickerModeDate];
        
        //设置初始pickerview显示的位置
        if (_currentDate != nil) {
            [_datePickerView setDate:_currentDate animated:NO];
        }
        
        [_datePickerView addTarget:self action:@selector(datePickerDateChanged:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_datePickerView];
        
        [self addToolBar:_datePickerView];
        
        [UIView animateWithDuration:0.5 animations:^{

            _datePickerView.frame = CGRectMake(0, 44, 320, 216);
            _toolBar.frame = CGRectMake(0, _datePickerView.frame.origin.y-44, 320, 44);
        }];
    }
    
}

- (void)pickerHide
{
    if (_type == UICustomPickerViewNormalType) {
        
        [UIView animateWithDuration:0.5 animations:^{
            self.alpha = 0.0;
            _normalPickerView.frame = CGRectMake(0, 216+44, 320, 216);
            _toolBar.frame = CGRectMake(0, _normalPickerView.frame.origin.y-44, 320, 44);
        }];
        
    }
    
    else if(_type == UICustomPickerViewAreaType){
        
        [UIView animateWithDuration:0.5 animations:^{
            self.alpha = 0.0;
            _areaPickerView.frame = CGRectMake(0, 216+44, 320, 216);
            _toolBar.frame = CGRectMake(0, _areaPickerView.frame.origin.y-44, 320, 44);
        }];
        
    }
    
    else if (_type == UICustomPickerViewDateType) {
        
        [UIView animateWithDuration:0.5 animations:^{
            self.alpha = 0.0;
            _datePickerView.frame = CGRectMake(0, 216+44, 320, 216);
            _toolBar.frame = CGRectMake(0, _datePickerView.frame.origin.y-44, 320, 44);
        }];
    }
    
    if([self.delegate respondsToSelector:@selector(customPickerViewDidDismiss:)]) {
        [self.delegate customPickerViewDidDismiss:self];
    }
}

- (void)confirmPickView
{
    if (_type == UICustomPickerViewNormalType) {
    
        [_delegate customPickerView:self didSelectRow:_selectedRow inComponent:_selectedComponent];
    }
    
    else if(_type == UICustomPickerViewAreaType){
        
        //回调给个人信息页面
        if (_areaString == nil) {

            _areaString = [NSString stringWithFormat:@"%@ %@ %@", _areaPickerView.locate.state, _areaPickerView.locate.city, _areaPickerView.locate.district];
            NSLog(@"%@-----",_areaString);
            _areaString = [_areaString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        }

        [_delegate customPickerView:self areaStringReceived:_areaString];
    }
    
    else if (_type == UICustomPickerViewDateType) {
        
        if (_currentDate == nil) {
            
            _currentDate = _datePickerView.date;
        }
        
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        [dateFormatter setLocale:[NSLocale currentLocale]];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
        
        NSString *dateString = [dateFormatter stringFromDate:_currentDate];
        
        [_delegate customPickerViewDateChanged:dateString];
    }
    [self pickerHide];
}

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
{
    _selectedRow = row;
    _selectedComponent = component;
    [_normalPickerView selectRow:row inComponent:component animated:animated];
}

- (void)addToolBar:(UIView *)view
{
    
    //创建工具栏
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];
	UIBarButtonItem *confirmBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(confirmPickView)];
	UIBarButtonItem *flexibleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
	UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(pickerHide)];
    [items addObject:cancelBtn];
    [items addObject:flexibleSpaceItem];
    [items addObject:confirmBtn];
    
    
    if (_toolBar == nil) {
        _toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, view.frame.origin.y - 44, 320, 44)];
    }
    _toolBar.hidden = NO;
    _toolBar.barStyle = UIBarStyleBlackTranslucent;
    _toolBar.items = items;
    
    items = nil;
    [self addSubview:_toolBar];
    
}


#pragma mark - UIPickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return [_dataSource numberOfComponentsInUICustomPickerView:self];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_dataSource customPickerView:self numberOfRowsInComponent:component];
}

#pragma mark - UIPickerView Delegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    return [_delegate customPickerView:self titleForRow:row forComponent:component];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component
{
    _selectedRow = row;
    _selectedComponent = component;
}

#pragma mark - HZAreaPicker delegate

-(void)pickerDidChangeStatus:(HZAreaPickerView *)picker
{
    _areaString = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];
    
    NSLog(@"%d", [_areaString length]);
    
    _areaString = [_areaString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSLog(@"%d", [_areaString length]);
}

#pragma mark - DatePicker 事件

- (void)datePickerDateChanged:(UIDatePicker *)datePicker
{
    _currentDate = datePicker.date;
    
}
@end
